package com.estar.assistsurvy.activity;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.widget.SwitchButton;
import com.estar.assistsurvy.widget.SwitchButton.OnChangeListener;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

public class SettingsActivity extends BaseActivity implements OnClickListener{

	private LinearLayout returnBack;
	private SwitchButton GPS_switch;
	private int isOpen=1;
	private Intent intent;
	private SharedPreferences share;
	private Editor editor;
	private String uuid;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		init();
	}
	private void init(){
		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);
		GPS_switch=find(R.id.GPS_switch);
		//		GPS_switch.setOnClickListener(this);
		share=getSharedPreferences("user", MODE_PRIVATE);
		uuid=share.getString("uuid", "");
		share=getSharedPreferences("GPS_switch", MODE_PRIVATE);
		editor=share.edit();
		isOpen=share.getInt(uuid, 1);
		if(isOpen==1){
			GPS_switch.setSwitchOn(true);
		}else if(isOpen==0){
			GPS_switch.setSwitchOn(false);
		}
		
		GPS_switch.setOnChangeListener(new OnChangeListener() {

			@Override
			public void onChange(SwitchButton sb, boolean state) {
				// TODO Auto-generated method stub
				Log.d("switchButton", state ? "开":"关");  
				Toast.makeText(SettingsActivity.this, state ? "开":"关", Toast.LENGTH_SHORT).show();  
				if(state){
					intent = new Intent();
					intent.setAction("com.estar.receiver.GPS_OPEN");
					sendBroadcast(intent);
					
					editor.putInt(uuid, 1);
					editor.commit();
				}else{
					intent = new Intent("com.estar.receiver.GPS_CLOSE");
					sendBroadcast(intent);
					
					editor.putInt(uuid, 0);
					editor.commit();
				}
			}
		});
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			finish();
			break;

			//		case R.id.GPS_switch:
			//			if(isOpen){
			//				Intent intent = new Intent();
			//				intent.setAction("com.estar.receiver.GPS_CLOSE");
			//				sendBroadcast(intent);
			//				isOpen=false;
			//			}else{
			//				Intent intent=new Intent("com.estar.receiver.GPS_OPEN");
			//				sendBroadcast(intent);
			//				isOpen=true;
			//			}

			//			break;

		default:
			break;
		}
	}
}
